﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  DishManagement.aspx.cs
 作    者:范徐超
 编写日期:2009-09-14
 审    核:
 审核日期:
 描    述:菜式信息管理页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Business_Booking_DishManagement : WebUI.Page.ManagePage
{
    private Cisoft.Booking.Model.DishMO dishMO = new Cisoft.Booking.Model.DishMO();
    private Cisoft.Booking.BLL.DishBO dishBO = new Cisoft.Booking.BLL.DishBO();

    private int pageCount = 0;

    #region 页面加载事件
    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            GetList();
        }
    }
    #endregion

    #region 删除事件 protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList myArrayList = getIDs(GridView1, "cb_id", "lbl_DishID");
        if (myArrayList != null && myArrayList.Count != 0)
        {
            Delete(myArrayList);
            GetList();
        }
        else
        {
            string str = "请选择要删除的记录！";
            PageError(str, "返回操作");
        }
    }

    #endregion

    #region 根据ID号成批删除 public virtual void DeleteBated(ArrayList myArrayList)
    /// <summary>
    /// 根据ID号成批删除
    /// </summary>
    /// <param name="al">ID号列表</param>
    public void Delete(ArrayList myArrayList)
    {
        int id;
        for (int i = 0; i < myArrayList.Count; i++)
        {
            //获取的包厢ID
            id = int.Parse(myArrayList[i].ToString());
            //删除本包厢
            if (dishBO.Delete(id) > 0)
            {
                PageRight("删除成功！", "DishManagement.aspx", "返回", "");
            }
            else
            {
                PageError("删除失败！", "DishManagement.aspx");
            }
        }
    }
    #endregion

    #region 新增事件  protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    /// <summary>
    /// 新增事件
    /// </summary>
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("DishAdd.aspx");
    }
    #endregion

    #region 显示数据 private void GetList()
    /// <summary>
    /// 显示数据
    /// </summary>
    private void GetList()
    {
        DataSet ds = dishBO.GetAllList();
        GridView1.DataSource = ds;
        pageCount = ds.Tables[0].Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            GridView1.DataBind();
            PageStatus();
        }
        else
        {
            ViewState["numbers"] = "0";
            PageStatus();
        }
    }

    private void GetList(string strWhere)
    {
        DataSet ds = dishBO.GetList(" DishName like '%" + strWhere + "%' ");
        GridView1.DataSource = ds;
        pageCount = ds.Tables[0].Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            GridView1.DataBind();
            PageStatus();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
            ViewState["numbers"] = "0";
            PageStatus();
        }
    }
    #endregion

    #region 翻页事件
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        PagerEvent(sender, e, GridView1);
        GetList();
    }
    #endregion

    #region DeleteLevel 删除所有下级
    /// <summary>
    /// DeleteLevel 删除所有下级
    /// </summary>
    /// <param name="id"></param>
    public void DeleteLevel(int id)
    {
        Cisoft.IntegrationSystem.BLL.UserPowerClassBO bllUserPowerClassfordelete = new Cisoft.IntegrationSystem.BLL.UserPowerClassBO();
        Cisoft.IntegrationSystem.BLL.UserPowerBO bllUserPowerfordeleteclass = new Cisoft.IntegrationSystem.BLL.UserPowerBO();
        DataSet dst = new DataSet();
        dst = bllUserPowerClassfordelete.GetParent(id);
        if (dst.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < dst.Tables[0].Rows.Count; i++)
            {
                DataSet dst_b = new DataSet();
                dst_b = bllUserPowerClassfordelete.GetParent(int.Parse(dst.Tables[0].Rows[i]["UPC_ClassID"].ToString()));
                if (dst_b.Tables[0].Rows.Count > 0)
                {
                    DeleteLevel(int.Parse(dst.Tables[0].Rows[i]["UPC_ClassID"].ToString()));
                }
                else
                {

                    DataSet dsforuserpowerdelete = bllUserPowerfordeleteclass.GetList("UP_PowerClass_Rid=" + int.Parse(dst.Tables[0].Rows[i]["UPC_ClassID"].ToString()));

                    if (dsforuserpowerdelete.Tables[0].Rows.Count > 0)
                    {

                    }
                    else
                    {
                        bllUserPowerClassfordelete.Delete(int.Parse(dst.Tables[0].Rows[i]["UPC_ClassID"].ToString()));

                        #region 删除栏目附属更新管理员角色栏目串,后续逻辑事务补完

                        Cisoft.IntegrationSystem.BLL.UserRoleBO blluserrole = new Cisoft.IntegrationSystem.BLL.UserRoleBO();
                        Cisoft.IntegrationSystem.Model.UserRoleMO modeluserrole = new Cisoft.IntegrationSystem.Model.UserRoleMO();
                        DataSet dsforadd = blluserrole.GetList("");
                        int j = -1;
                        //取出所有会员角色
                        for (int i3 = 0; i3 < dsforadd.Tables[0].Rows.Count; i3++)
                        {
                            j = int.Parse(dsforadd.Tables[0].Rows[i3]["UR_RoleID"].ToString());
                            modeluserrole = blluserrole.GetModel(j);                          //获取会员角色model
                            //将角色菜单串转换为数组
                            string[] strPowerarray;
                            strPowerarray = Abruption(modeluserrole.UserRolePowerClass);
                            //对比，若相同则为空
                            for (int i2 = 0; i2 < strPowerarray.Length; i2++)
                            {
                                if (strPowerarray[i2] == dst.Tables[0].Rows[i]["UPC_ClassID"].ToString())
                                {
                                    strPowerarray[i2] = "";
                                }
                            }
                            //重新拼凑字符串
                            string endstring = "";
                            for (int j2 = 0; j2 < strPowerarray.Length; j2++)
                            {
                                if (strPowerarray[j2] == "")
                                {
                                }
                                else
                                {
                                    endstring += strPowerarray[j2] + ",";
                                }
                            }
                            //形成新串 并更新到数据库中
                            if (endstring.Length > 2)
                            {
                                endstring = endstring.Substring(0, endstring.Length - 1);  //去除最后一个逗号
                            }
                            else
                            {
                                endstring = endstring.Replace(",", "");
                            }

                            modeluserrole.UserRolePowerClass = endstring;

                            blluserrole.EditRecord(modeluserrole);
                            //btcheck += bllmemberrole.EditRecord(modelmemberrole);                       //更新栏目
                        }
                        #endregion

                    }
                    //DeleteUser(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
                }
                //bllgroup.Delete(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
                //DeleteUser(int.Parse(dst.Tables[0].Rows[i]["UG_GroupID"].ToString()));
            }
        }

        DataSet dsforuserpowerdelete2 = bllUserPowerfordeleteclass.GetList("UP_PowerClass_Rid=" + id);

        if (dsforuserpowerdelete2.Tables[0].Rows.Count > 0)
        {
        }
        else
        {
            bllUserPowerClassfordelete.Delete(id);


            #region 删除栏目附属更新会员角色栏目串,后续逻辑事务补完

            Cisoft.IntegrationSystem.BLL.UserRoleBO blluserrole = new Cisoft.IntegrationSystem.BLL.UserRoleBO();

            Cisoft.IntegrationSystem.Model.UserRoleMO modeluserrole = new Cisoft.IntegrationSystem.Model.UserRoleMO();

            DataSet dsforadd = blluserrole.GetList("");

            int j = -1;
            //取出所有会员角色

            for (int i = 0; i < dsforadd.Tables[0].Rows.Count; i++)
            {
                j = int.Parse(dsforadd.Tables[0].Rows[i]["UR_RoleID"].ToString());

                modeluserrole = blluserrole.GetModel(j);                          //获取会员角色model

                //将角色菜单串转换为数组

                string[] strPowerarray;
                strPowerarray = Abruption(modeluserrole.UserRolePowerClass);
                //对比，若相同则为空
                for (int i2 = 0; i2 < strPowerarray.Length; i2++)
                {
                    if (strPowerarray[i2] == id.ToString())
                    {
                        strPowerarray[i2] = "";
                    }

                }
                //重新拼凑字符串
                string endstring = "";
                for (int j2 = 0; j2 < strPowerarray.Length; j2++)
                {
                    if (strPowerarray[j2] == "")
                    {
                    }
                    else
                    {
                        endstring += strPowerarray[j2] + ",";
                    }
                }

                //形成新串 并更新到数据库中
                if (endstring.Length > 2)
                {
                    endstring = endstring.Substring(0, endstring.Length - 1);  //去除最后一个逗号
                }
                else
                {
                    endstring = endstring.Replace(",", "");
                }
                modeluserrole.UserRolePowerClass = endstring;
                blluserrole.EditRecord(modeluserrole);
                //btcheck += bllmemberrole.EditRecord(modelmemberrole);                       //更新栏目
            }
            #endregion
        }
        //bllgroup.Delete(id);
        //DeleteUser(id);
    }

    #endregion

    #region gridview中的事件

    #region RowDataBound事件 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标在列表行中停留时的样式
        for (int i = -1; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                Label Lbl_RuleReply = (Label)e.Row.FindControl("Lbl_RuleReply");
            }
        }

        //显示行号
        AddNo(GridView1, "lbl_No");

    }
    #endregion

    #region PageIndexChanging事件 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    /// <summary>
    /// PageIndexChanging事件
    /// </summary>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    #endregion

    #region 修改事件
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Edit(sender, e);
    }
    #endregion

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    #endregion

    #region 修改事件
    private void Edit(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            Response.Redirect("DishEdit.aspx?DishID=" + e.CommandArgument);
        }
    }
    #endregion

    #region 查询事件
    protected void imgbtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        string key = tb_Search.Text.Trim();
        if (!string.IsNullOrEmpty(key))
        {
            GetList(key);
        }
        else
        {
            GetList();
        }
    }
    #endregion

    #region 按钮和翻页事件 void PageStatus()
    private void PageStatus()
    {
        butState(GridView1, btnFirst, btnPrev, btnNext, btnLast);
        ShowStats(GridView1, lblCurrentIndex, lblNumbers, lblPageCount);
    }
    #endregion
}
